﻿Imports System.IO
Imports Ionic.Zip

Module modExtract
    Public Function Zip_ExtractFile(ByVal zipPath As String, ByVal outputDirectory As String, ByVal fileName As String) _
        As en_resultZipExtract

        Dim tmpResult As en_resultZipExtract

        If File.Exists(zipPath) = False Then

            tmpResult = en_resultZipExtract.zipFileNotFound
            Zip_ExtractFile = tmpResult
            Exit Function

        End If

        If Directory.Exists(outputDirectory) = False Then

            If String_IsEmpty(outputDirectory) = False Then
                Directory.CreateDirectory(outputDirectory)
            End If

        End If

        If Directory.Exists(outputDirectory) = False Then

            tmpResult = en_resultZipExtract.zipDirNotFound
            Return tmpResult

        End If

        Using zip As ZipFile = ZipFile.Read(zipPath)

            Dim eObj As ZipEntry

            For Each eObj In zip
                If eObj.FileName.Contains(fileName) Then
                    eObj.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently)
                End If
            Next

        End Using

        tmpResult = en_resultZipExtract.ziSuccess

        Return tmpResult
    End Function

    Public Function Zip_Extract(ByVal fileName As String, ByVal outputDirectory As String) As en_resultZipExtract

        Dim tmpResult As en_resultZipExtract

        If File.Exists(fileName) = False Then

            tmpResult = en_resultZipExtract.zipFileNotFound
            Zip_Extract = tmpResult
            Exit Function

        End If

        If Directory.Exists(outputDirectory) = False Then

            If String_IsEmpty(outputDirectory) = False Then
                Directory.CreateDirectory(outputDirectory)
            End If

        End If

        If Directory.Exists(outputDirectory) = False Then

            tmpResult = en_resultZipExtract.zipDirNotFound
            Return tmpResult

        End If

        Using zip As ZipFile = ZipFile.Read(fileName)

            Dim eObj As ZipEntry

            For Each eObj In zip

                eObj.Extract(outputDirectory, ExtractExistingFileAction.OverwriteSilently)

            Next

        End Using

        tmpResult = en_resultZipExtract.ziSuccess

        Return tmpResult
    End Function
End Module